package com.yoo.ons.dialog

import android.app.Activity
import android.graphics.Color
import android.os.Build
import android.view.animation.AnimationUtils
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentActivity
import com.github.gzuliyujiang.wheelpicker.widget.OptionWheelLayout
import com.github.gzuliyujiang.wheelview.annotation.CurtainCorner
import com.gyf.immersionbar.ImmersionBar
import com.yoo.module_base.utils.ScreenUtils
import com.yoo.module_base.utils.setOnClickCallback
import com.yoo.module_base.utils.setSystemBottomNavBarColor
import com.yoo.module_base.utils.setSystemBottomNavBarNoColor
import com.yoo.ons.R
import razerdp.basepopup.BasePopupWindow


class UpdateHeightPopup(activity: FragmentActivity, var height:String, var callBack:(name:String)->Unit):BasePopupWindow(activity) {

    private var selectDateValue=""
    //身高列表
    private val heightArrayList = arrayListOf("3\'07\"","3\'08\"","3\'09\"","3\'10\"","3\'11\"","3\'12\""
        ,"4\'00\"","4\'01\"","4\'02\"","4\'03\"","4\'04\"","4\'05\"","4\'06\"","4\'07\"","4\'08\"","4\'09\"","4\'10\"","4\'11\"","4\'12\"",
        "5\'00\"","5\'01\"","5\'02\"","5\'03\"","5\'04\"","5\'05\"","5\'06\"","5\'07\"","5\'08\"","5\'09\"","5\'10\"","5\'11\"","5\'12\"",
        "6\'00\"","6\'01\"","6\'02\"","6\'03\"","6\'04\"","6\'05\"","6\'06\"","6\'07\"","6\'08\"","6\'09\"","6\'10\"","6\'11\"","6\'12\"",
        "7\'00\"","7\'01\"","7\'02\"","7\'03\"","7\'04\"","7\'05\"","7\'06\"","7\'07\"","7\'08\"","7\'09\"","7\'10\"")
    //默认定位中间值
    private var defaultPosition=30

    init {
        setContentView(R.layout.popup_update_height)
        initView(activity)
        showAnimation=  AnimationUtils.loadAnimation(activity, com.yoo.module_res.R.anim.anims_slide_in_bottom)
        dismissAnimation=  AnimationUtils.loadAnimation(activity, com.yoo.module_res.R.anim.anims_slide_out_bottom)
    }

    fun initView(activity:FragmentActivity){
        val popupClose = findViewById<ImageView>(R.id.popup_close)
        val dateSelectLayout = findViewById<OptionWheelLayout>(R.id.height_select_layout)
        val popupUpdateBirthDateSave = findViewById<TextView>(R.id.popup_update_birth_date_save)
        popupClose.setOnClickCallback {
            dismiss()
        }

        //身高选择器设置值
        dateSelectLayout.setData(heightArrayList)
        dateSelectLayout.setVisibleItemCount(5)
        dateSelectLayout.setItemSpace(ScreenUtils.dip2px(activity,45f))
        dateSelectLayout.setAtmosphericEnabled(false)
        dateSelectLayout.setIndicatorEnabled(false)
        dateSelectLayout.setCurtainEnabled(true)
        dateSelectLayout.setCurtainCorner(CurtainCorner.ALL)
        dateSelectLayout.setCurtainRadius(ScreenUtils.dp2pxF(activity,14f))
        dateSelectLayout.setCurtainColor(ContextCompat.getColor(activity, com.yoo.module_res.R.color.color_551066))
        dateSelectLayout.setTextColor(Color.WHITE)
        dateSelectLayout.setSelectedTextColor(Color.WHITE)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            dateSelectLayout.wheelView.typeface = context.resources.getFont(com.yoo.module_res.R.font.lexenddeca_semibold)
        }
        dateSelectLayout.setOnOptionSelectedListener { _, item ->
            selectDateValue="$item"
        }
        //当前身高已选择，寻找当前的身高索引
        for (i in 0 until heightArrayList.size){
            if (height== heightArrayList[i]){
                defaultPosition=i
                break
            }
        }
        //获取当前选择的身高或者默认的身高值
        selectDateValue = heightArrayList[defaultPosition]
        dateSelectLayout.setDefaultPosition(defaultPosition)

        popupUpdateBirthDateSave.setOnClickCallback {
            callBack.invoke(selectDateValue)
            dismiss()
        }



        //设置底部导航栏颜色
        setSystemBottomNavBarColor(activity)
        onDismissListener =object : OnDismissListener() {
            override fun onDismiss() {
                setSystemBottomNavBarNoColor(activity)
            }

        }
    }
}